package com.pinc.akb48;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

public class Parser_Version {
	private SAXParserFactory spf;
	private SAXParser sp;
	private XMLReader xr;
	private XMLHandler_Version xhd;

	class SoftVer {
		public String name;
		public int version;
		public String feature;
	}

	public Parser_Version() {
		spf = SAXParserFactory.newInstance();
		try {
			/* Handling XML */
			sp = spf.newSAXParser();
			xr = sp.getXMLReader();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		}
	}

	public boolean parse(String appName) {
		/* URL */
		try {
			InputStream is = MyHTTPHelper.ConnectURL(
					"http://pinc-version.appspot.com", 2000);
			if (is == null)
				return false;
			xhd = new XMLHandler_Version(appName);
			xr.setContentHandler(xhd);
			xr.parse(new InputSource(is));
			return true;
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		}
		return false;
	}

	public int getVersion() {
		return xhd.sv.version;
	}

}
